package backend.com.zyc.comm.config;

import java.io.Reader;
import java.util.Properties;

import util.Constants;

import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;

public class DaoConfig {

	private static final String resource = "backend/com/zyc/resource/dao.xml";

	private static DaoManager instance = null;

	private static void initDaoManager() {
		try {
			Reader reader = Resources.getResourceAsReader(resource);
			Properties props = Resources
					.getResourceAsProperties(Constants.DATABASE_PROPERTIES);
			instance = DaoManagerBuilder.buildDaoManager(reader, props);

			// String url = props.getProperty("url");
			// String driver = props.getProperty("driver");
			// String username = props.getProperty("username");
			// String password = props.getProperty("password");
			// Log.getLogger(DaoConfig.class);
			// Class.forName(driver).newInstance();
			// Connection conn = DriverManager.getConnection(url, username,
			// password);
			// // Connection conn = DriverManager.getConnection(url);
			// ScriptRunner runner = new ScriptRunner(conn, false, false);
			// //
			// PropertyConfigurator.configure(ResouceLoader.getResouce(Constant.LOG4J_FILE));
			// runner.setErrorLogWriter(null);
			// runner.setLogWriter(null);
			// //
			// runner.runScript(Resources.getResourceAsReader("ddl/hsql/jpetstore-hsqldb-schema.sql"));
			// //
			// runner.runScript(Resources.getResourceAsReader("ddl/hsql/jpetstore-hsqldb-dataload.sql"));
		} catch (Exception e) {
			throw new RuntimeException("Description.  Cause: " + e, e);
		}

	}

	public static DaoManager getDaoManager() {
		if (instance == null) {
			initDaoManager();
		}
		return instance;
	}

}
